home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / MDATES.ZIP / MDATES.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-08  |  20.8 KB  |  650 lines

  1. //*                               MDATES.CPP                               *
  2. //*                                                                        *
  3. //*     C++ Date Class for Various Date Oriented Routines                  *
  4. //*                                                                        *
  5. //*    Written By   : Matthew Rhoades                                      *
  6. //*    Last Update  : February, 1994                                       *
  7. //*                                                                        *
  8. //**************************************************************************
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <dos.h>
  13. #include <string.h>
  14. #include <iostream.h>
  15. #include <alloc.h>
  16. #include <process.h>
  17. #include "mdates.h"
  18.  
  19.  
  20. //**************************************************************************
  21. //                        MDates Constructor                               *
  22. //**************************************************************************
  23. MDates::MDates()
  24. {
  25.      Day        =  0;                  //  Initialize the
  26.      Month      =  0;                  //  Class Variables
  27.      Year       =  0;
  28.      Julian     =  0;
  29.      NewDate    =  0;
  30.      lTestDate  =  0;
  31.      Lowdate    =  0;
  32.      Highdate   =  0;
  33.      Days       =  0;
  34.      cDay       = new char[3];
  35.      cMonth     = new char[3];
  36.      cYear      = new char[3];
  37.      cChk       = new char[9];
  38.      cTestDate  = new char[9];
  39.      Buffer     = new char[35];
  40.  
  41.      memset(cDay, '\0', 3);
  42.      memset(cMonth, '\0', 3);
  43.      memset(cYear, '\0', 3);
  44.      memset(cChk, '\0', 9);
  45.      memset(cTestDate, '\0', 9);
  46.      memset(Buffer, '\0', 35);
  47. }
  48.  
  49. //**************************************************************************
  50. //                        MDates Destructor                                *
  51. //**************************************************************************
  52. MDates::~MDates()
  53. {
  54.    if(cDay)       delete cDay;               // Destroy the
  55.    if(cMonth)     delete cMonth;             // Class Variables
  56.    if(cYear)      delete cYear;              // Freeing Memory
  57.    if(cChk)       delete cChk;
  58.    if(cTestDate)  delete cTestDate;
  59.    if(Buffer)     delete Buffer;
  60. }
  61.  
  62. //**************************************************************************
  63. //                        DateFormat Function                              *
  64. //                                                                         *
  65. //   Pass it  : char * CCYYMMDD                                            *
  66. //   Support formats are:                                                  *
  67. //                           STANDARD   :  MM/DD/YY                        *
  68. //                           MMDDYY     :  same                            *
  69. //                           YYMMDD     :  same                            *
  70. //                                                                         *
  71. //    EX:   DateFormat( "19940101", result, STANDARD);                     *
  72. //          result  = 01/01/94                                             *
  73. //**************************************************************************
  74. void MDates::DateFormat(char *dt, char *result, int format)
  75. {
  76.     cYear[0]  = dt[2];  cYear[1]  = dt[3];  cYear[2]  = '\0';
  77.     cMonth[0] = dt[4];  cMonth[1] = dt[5];  cMonth[2] = '\0';
  78.     cDay[0]   = dt[6];  cDay[1]   = dt[7];  cDay[2]   = '\0';
  79.  
  80.     memset( result, '\0', sizeof(result));
  81.  
  82.     switch(format)
  83.     {
  84.         case STANDARD :
  85.              {
  86.                   result[0] = cMonth[0];
  87.                   result[1] = cMonth[1];
  88.                   result[2] = SLASH;
  89.                   result[3] = cDay[0];
  90.                   result[4] = cDay[1];
  91.                   result[5] = SLASH;
  92.                   result[6] = cYear[0];
  93.                   result[7] = cYear[1];
  94.                   result[8] = '\0';
  95.                   break;
  96.              }
  97.         case MMDDYY:
  98.              {
  99.                   result[0] = cMonth[0];
  100.                   result[1] = cMonth[1];
  101.                   result[2] = cDay[0];
  102.                   result[3] = cDay[1];
  103.                   result[4] = cYear[0];
  104.                   result[5] = cYear[1];
  105.                   result[6] = '\0';
  106.                   break;
  107.              }
  108.         case YYMMDD:
  109.              {
  110.                   result[0] = cYear[0];
  111.                   result[1] = cYear[1];
  112.                   result[2] = cMonth[0];
  113.                   result[3] = cMonth[1];
  114.                   result[4] = cDay[0];
  115.                   result[5] = cDay[1];
  116.                   result[6] = '\0';
  117.                   break;
  118.              }
  119.     }
  120. }
  121.  
  122. //**************************************************************************
  123. //                        InitDate Function                                *
  124. //                  (Opposite of DateFormat Function)                      *
  125. //                                                                         *
  126. //   Pass it  : STANDARD, MMDDYY, YYMMDD                                   *
  127. //   Support formats are:                                                  *
  128. //                           STANDARD   :  MM/DD/YY                        *
  129. //                           MMDDYY     :  same                            *
  130. //                           YYMMDD     :  same                            *
  131. //                                                                         *
  132. //    EX:   DateFormat( "01/01/94", result, STANDARD);                     *
  133. //          result = "19940101"                                            *
  134. //**************************************************************************
  135. void MDates::InitDate(char *dt, char *result, int format)
  136. {
  137.     memset( result, '\0', sizeof(result));
  138.  
  139.     switch(format)
  140.     {
  141.         case STANDARD :
  142.              {
  143.                  cYear[0]  = dt[6];  cYear[1]  = dt[7];  cYear[2]  = '\0';
  144.                  cMonth[0] = dt[0];  cMonth[1] = dt[1];  cMonth[2] = '\0';
  145.                  cDay[0]   = dt[3];  cDay[1]   = dt[4];  cDay[2]   = '\0';
  146.  
  147.                  result[0]  = '1';
  148.                  result[1]  = '9';
  149.                  result[2]  = cYear[0];
  150.                  result[3]  = cYear[1];
  151.                  result[4]  = cMonth[0];
  152.                  result[5]  = cMonth[1];
  153.                  result[6]  = cDay[0];
  154.                  result[7]  = cDay[1];
  155.                  result[8]  = '\0';
  156.                  break;
  157.              }
  158.         case MMDDYY:
  159.              {
  160.                  cYear[0]  = dt[4];  cYear[1]  = dt[5];  cYear[2]  = '\0';
  161.                  cMonth[0] = dt[0];  cMonth[1] = dt[1];  cMonth[2] = '\0';
  162.                  cDay[0]   = dt[2];  cDay[1]   = dt[3];  cDay[2]   = '\0';
  163.  
  164.                  result[0]  = '1';
  165.                  result[1]  = '9';
  166.                  result[2]  = cYear[0];
  167.                  result[3]  = cYear[1];
  168.                  result[4]  = cMonth[0];
  169.                  result[5]  = cMonth[1];
  170.                  result[6]  = cDay[0];
  171.                  result[7]  = cDay[1];
  172.                  result[8]  = '\0';
  173.                  break;
  174.              }
  175.         case YYMMDD:
  176.              {
  177.                  cYear[0]  = dt[0];  cYear[1]  = dt[1];  cYear[2]  = '\0';
  178.                  cMonth[0] = dt[2];  cMonth[1] = dt[3];  cMonth[2] = '\0';
  179.                  cDay[0]   = dt[4];  cDay[1]   = dt[5];  cDay[2]   = '\0';
  180.  
  181.                  result[0]  = '1';
  182.                  result[1]  = '9';
  183.                  result[2]  = cYear[0];
  184.                  result[3]  = cYear[1];
  185.                  result[4]  = cMonth[0];
  186.                  result[5]  = cMonth[1];
  187.                  result[6]  = cDay[0];
  188.                  result[7]  = cDay[1];
  189.                  result[8]  = '\0';
  190.                  break;
  191.              }
  192.     }
  193. }
  194.  
  195. //**************************************************************************
  196. //                          Date2Long Function                             *
  197. //**************************************************************************
  198. long MDates::Date2Long(char *date)            // Date must be in CCYYMMDD
  199. {                                             // Format
  200.     DateFormat( date, cChk, MMDDYY);
  201.  
  202.     if ( !Valid_Date(cChk) )
  203.     {
  204.     return(-1);
  205.     }
  206.  
  207. return( atol(date) );
  208. }
  209.  
  210. //**************************************************************************
  211. //                          Date2String Function                           *
  212. //**************************************************************************
  213. int MDates::Date2String(long date, char *newdate)  // Date must be in CCYYMMDD
  214. {                                                  // Format
  215.     ltoa( date, Buffer, 10);
  216.     DateFormat(Buffer, cChk, MMDDYY);
  217.  
  218.     if( !Valid_Date( cChk ) )
  219.           return -256;
  220.  
  221.     strcpy(newdate, Buffer);
  222. return 1;
  223. }
  224.  
  225.  
  226. //**************************************************************************
  227. //                        Date2Julian Function                             *
  228. //**************************************************************************
  229. long MDates::Date2Julian(char *date)            // Date must be in YYYYMMDD
  230. {                                               // Format
  231.     cDay[0]   = date[6];
  232.     cDay[1]   = date[7];       // Parse the date
  233.     cDay[2]   = '\0';
  234.     cMonth[0] = date[4];
  235.     cMonth[1] = date[5];
  236.     cMonth[2] = '\0';
  237.     cYear[0]  = date[2];
  238.     cYear[1]  = date[3];
  239.     cYear[2]  = '\0';
  240.  
  241.     Day   = atoi(cDay);
  242.     Month = atoi(cMonth);
  243.     Year  = atoi(cYear);
  244.  
  245.  
  246. return( DMYtoJulian( Day, Month, Year ) );
  247. }
  248.  
  249. //**************************************************************************
  250. //                DMYtoJulian Function -  Low Level                        *
  251. //**************************************************************************
  252. long MDates::DMYtoJulian( int day, int month, int year )
  253. {
  254.    int a, b;
  255.    float year_corr;
  256.  
  257.    itoa(year, Buffer, 10);
  258.    if(year < 10)
  259.    {
  260.       cYear[0] = '0';
  261.       cYear[1] = Buffer[0];
  262.       cYear[2] = '\0';
  263.    }
  264.    else { strcpy(cYear, Buffer); }
  265.      itoa(month, Buffer, 10);
  266.      if(month < 10)
  267.      {
  268.         cMonth[0] = '0';
  269.         cMonth[1] = Buffer[0];
  270.         cMonth[2] = '\0';
  271.      }
  272.      else { strcpy(cMonth, Buffer); }
  273.        itoa(day, Buffer, 10);
  274.        if(day < 10)
  275.        {
  276.           cDay[0] = '0';
  277.           cDay[1] = Buffer[0];
  278.           cDay[2] = '\0';
  279.        }
  280.        else { strcpy(cDay, Buffer); }
  281.  
  282.     Buffer[0] =  '\0';
  283.     strcpy(Buffer, cMonth);
  284.     strcat(Buffer, cDay);
  285.     strcat(Buffer, cYear);
  286.  
  287.        if( !Valid_Date( Buffer ) )
  288.              return -1;
  289.  
  290.         year_corr = (year > 0 ? 0.0 : 0.75 );
  291.         if(month <= 2)
  292.         {
  293.            year--;
  294.            month += 12;
  295.         }
  296.  
  297.    b = 0;
  298.  
  299.    if( year * 10000.0 + month * 100.0 + day >= 15821015.0 )
  300.    {
  301.       a = year / 100;
  302.       b = (2 - a) + (a / 4);
  303.    }
  304.  
  305.    Julian = (long) (365.25 * year - year_corr) + (long)(30.6001 * (month + 1)) +
  306.             day + 1720994L + b;
  307.  
  308. return( Julian );
  309. }
  310.  
  311. //**************************************************************************
  312. //                      Julian2String Function
  313. //**************************************************************************
  314. void MDates::Julian2String(long Julian, char *string)
  315. {
  316.    NewDate = Julian2Long( Julian );         // returns CCYYMMDD Format char*
  317.    ltoa(NewDate, Buffer, 10);
  318.    strcpy(string, Buffer);
  319. }
  320.  
  321.  
  322. //**************************************************************************
  323. //                        Julian2Long Function
  324. //**************************************************************************
  325. long MDates::Julian2Long(long Julian)
  326. {
  327.    long a, b, c, d, e, z, alpha;
  328.                                         // returns CCYYMMDD Format long
  329.     z = Julian + 1;
  330.  
  331.      if(z < 2299161L)
  332.         a = z;
  333.      else
  334.      {
  335.         alpha = (long) ((z - 1867216.25) / 36524.25);
  336.         a = z + 1 + alpha - (alpha / 4);
  337.      }
  338.  
  339.    b = a + 1524;
  340.    c = (long) ((b - 122.1) / 365.25);
  341.    d = (long) ( 365.25 * c );
  342.    e = (long) (( b - d) / 30.6001);
  343.  
  344.    Day     = (int) b - d - (long)(30.6001 * e);
  345.    Month   = (int) (e < 13.5) ? e - 1 : e - 13;
  346.    Year    = (int) (Month > 2.5) ? (c - 4716) : c - 4715;
  347.  
  348.    memset( cTestDate, '\0', sizeof(cTestDate));
  349.    cTestDate[0] = '1';
  350.    cTestDate[1] = '9';
  351.    itoa(Year, Buffer, 10);
  352.    if(Year < 10)
  353.    {
  354.       cTestDate[2] = '0';
  355.       cTestDate[3] = Buffer[0];
  356.    }
  357.    else { strcat(cTestDate, Buffer); }
  358.      itoa(Month, Buffer, 10);
  359.      if(Month < 10)
  360.      {
  361.         cTestDate[4] = '0';
  362.         cTestDate[5] = Buffer[0];
  363.      }
  364.      else { strcat(cTestDate, Buffer); }
  365.        itoa(Day, Buffer, 10);
  366.        if(Day < 10)
  367.        {
  368.           cTestDate[6] = '0';
  369.           cTestDate[7] = Buffer[0];
  370.        }
  371.        else { strcat(cTestDate, Buffer); }
  372.        cTestDate[8] = '\0';
  373.  
  374.    NewDate = atol(cTestDate);
  375.  
  376. return(NewDate);
  377. }
  378.  
  379. //**************************************************************************
  380. //          IsValidDate Function  --  Checks Date Validity
  381. //**************************************************************************
  382. int MDates::IsValidDate( char *s )     // CCYYMMDD Format
  383. {
  384.     DateFormat(s, cTestDate, MMDDYY);
  385.     test = Valid_Date( cTestDate );
  386.     if(test)
  387.     {
  388.         return 1;
  389.     }
  390.     else
  391.        {
  392.            return -1;
  393.        }
  394. }
  395.  
  396. //**************************************************************************
  397. //                        Valid_Date Function
  398. //
  399. //   I Didn't write this function.... Downloaded it from Compuserve.....
  400. //   So I give the author full credit for this function....
  401. //
  402. //**************************************************************************
  403. int MDates::Valid_Date( char *s )     // MMDDYY Format
  404. {
  405.     int days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
  406.     char date[7];
  407.     int mo;
  408.     int da;
  409.     int yr;
  410.     int eo;
  411.  
  412.     strcpy(date, s);
  413.     if (date == NULL)
  414.             return 1;
  415.  
  416.     yr = atoi(date + 5);
  417.     *(date + 5) = '\0';
  418.     eo = atoi(date + 4);
  419.     if (eo == 0 || eo == 2 || eo == 6 || eo == 8)
  420.     {
  421.             if (yr == 0 || yr == 4 || yr == 8)
  422.                     days[1]++;
  423.     }
  424.     if (eo == 1 || eo == 3 || eo == 5 || eo == 7 || eo == 9)
  425.     {
  426.             if (yr == 2 || yr == 6)
  427.                     days[1]++;
  428.     }
  429.  
  430.     *(date + 4) = '\0';
  431.     da = atoi(date + 2);
  432.     *(date + 2) = '\0';
  433.     mo = atoi(date);
  434.  
  435.     if (mo && mo < 13 && da && da <= days [mo - 1])
  436.             return 1;
  437.  
  438. return 0;
  439. }
  440.  
  441. //**************************************************************************
  442. //        DaysBetween Function   --  Computes the Days Between Two Dates
  443. //**************************************************************************
  444. int MDates::DaysBetween(char *dateone, char *datetwo)
  445. {
  446.         Lowdate  = Date2Julian(dateone);
  447.         Highdate = Date2Julian(datetwo);
  448.  
  449.         if((Lowdate <= 0) || (Highdate <= 0))
  450.         {
  451.             return -1;    // One or Both dates invalid!
  452.         }
  453.  
  454.        Days = Highdate - Lowdate;
  455.  
  456. return( abs(Days) );
  457. }
  458.  
  459. //******************************************************************
  460. //*****  Date Function for getting Sys Date   **********************
  461. //******************************************************************
  462. void MDates::SystemDate(char *date)
  463. {
  464.    char TYear[5];                    //  returns CCYYMMDD char *
  465.  
  466.    struct dosdate_t dt;
  467.    _dos_getdate(&dt);
  468.  
  469.    itoa(dt.day, Buffer, 10);
  470.    if(dt.day < 10)
  471.    {
  472.       cDay[0] = '0';
  473.       cDay[1] = Buffer[0];
  474.       cDay[2] = '\0';
  475.    }
  476.    else { strcpy(cDay, Buffer); }
  477.      itoa(dt.month, Buffer, 10);
  478.      if(dt.month < 10)
  479.      {
  480.         cMonth[0] = '0';
  481.         cMonth[1] = Buffer[0];
  482.         cMonth[2] = '\0';
  483.      }
  484.      else { strcpy(cMonth, Buffer); }
  485.  
  486.      itoa(dt.year, Buffer, 10);
  487.      strcpy(TYear, Buffer);
  488.  
  489.      strcpy(date, TYear);
  490.      date[4] = cMonth[0];
  491.      date[5] = cMonth[1];
  492.      date[6] = cDay[0];
  493.      date[7] = cDay[1];
  494.      date[8] = '\0';
  495. }
  496.  
  497. //***************************************************************************
  498. //   PrintDate Function For Getting Formatted Date for Reports and Output   *
  499. //***************************************************************************
  500. void MDates::PrintDate(char *date)
  501. {
  502.    char day[3];                             // This Function gets the system
  503.    char year[5];                            // Date and returns it in char *
  504.    char month[4];                           // form... i.e.  PrintDate();
  505.                                             // returns ->  Mar 5, 1994
  506.    const char *months[12] =
  507.    {
  508.      "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  509.      "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  510.    };
  511.  
  512.       struct dosdate_t dt;
  513.       _dos_getdate(&dt);
  514.  
  515.       itoa(dt.day, day, 10);
  516.       itoa(dt.year, year, 10);
  517.       strcpy(date,months[dt.month - 1]);
  518.       strcat(date, " ");
  519.       strcat(date, day);
  520.       strcat(date, ", ");
  521.       strcat(date, year);
  522. }
  523.  
  524.  
  525.  
  526. //****************************************************************************
  527. //***** The Function to Subtract x Months from a Given Date  *****************
  528. //****************************************************************************
  529. long MDates::SubMonths(long date, int x)
  530. {
  531.     int nMonth;                    //  new month
  532.     int nYear;                     //  new year
  533.     int nDay;                      //  new day
  534.  
  535.     memset( cChk, '\0', sizeof(cChk));
  536.     memset( cTestDate, '\0', sizeof(cTestDate));
  537.  
  538.     ltoa (date, Buffer, 10);     //  Convert date to String
  539.  
  540.     cYear [0] = Buffer [2];      //  Parse out d,m,y from string into
  541.     cYear [1] = Buffer [3];      //  sub strings...
  542.     cYear [2] = '\0';
  543.     cMonth [0] = Buffer [4];
  544.     cMonth [1] = Buffer [5];
  545.     cMonth [2] = '\0';
  546.     cDay [0] = Buffer [6];
  547.     cDay [1] = Buffer [7];
  548.     cDay [2] = '\0';
  549.  
  550.     Year = atoi (cYear);           // Convert substrings to integers
  551.     Month = atoi (cMonth);         // for mathematical computations...
  552.     Day = atoi (cDay);
  553.  
  554.     nMonth = Month - x;            // Subtract x Months from Month;
  555.  
  556.     if (nMonth < 1)                // if new month is less than 1
  557.     {                              //
  558.         nMonth += 12;              // add 12 to new month
  559.         Year = Year--;             // subtract one from the year
  560.     }
  561.  
  562.     cTestDate[0] = '1';            // Build the cTestDate Var
  563.     cTestDate[1] = '9';            // to check for Date Validity
  564.     itoa (Year, Buffer, 10);
  565.      if(Year < 10)
  566.      {
  567.         cTestDate[2] = '0';
  568.         cTestDate[3] = Buffer[0];
  569.         cTestDate[4] = '\0';
  570.      }
  571.      else { strcat(cTestDate, Buffer); }
  572.      itoa (nMonth, Buffer,10);
  573.      if(nMonth < 10)
  574.      {
  575.         cTestDate[4] = '0';
  576.         cTestDate[5] = Buffer[0];
  577.         cTestDate[6] = '\0';
  578.      }
  579.      else { strcat(cTestDate, Buffer); }
  580.      itoa (Day, Buffer, 10);
  581.        if(Day < 10)
  582.        {
  583.            cTestDate[6] = '0';
  584.            cTestDate[7] = Buffer[0];
  585.        }
  586.        else { strcat(cTestDate, Buffer); }
  587.        cTestDate[8] = '\0';
  588.  
  589.     DateFormat(cTestDate, cChk, MMDDYY);
  590.  
  591.     while( !Valid_Date(cChk) )  // Test for date validity
  592.     {
  593.         cDay [0] = cTestDate [6];       // Get the Day of the month
  594.         cDay [1] = cTestDate [7];       // and Subtract one
  595.         cDay [2] = '\0';
  596.         Day = atoi (cDay);
  597.         Day--;
  598.  
  599.         itoa (Day, Buffer, 10);      // must use the larger Buffer
  600.                                         // var. for conversions, or
  601.                                         // you have memory problems
  602.         if (Day < 10)
  603.         {
  604.             cTestDate [6] = '0';
  605.             cTestDate [7] = Buffer [0];
  606.             cTestDate [8] = '\0';
  607.         }
  608.         else
  609.         {
  610.             cTestDate [6] = Buffer [0];         // Put the day back into
  611.             cTestDate [7] = Buffer [1];         // cTestDate....
  612.             cTestDate [8] = '\0';
  613.         }
  614.       DateFormat(cTestDate, cChk, MMDDYY);        // Format cChk and loop
  615.     }                                             // to retest date
  616.  
  617.     NewDate = atol (cTestDate);         // Convert to Long and return
  618.  
  619.     return (NewDate);
  620. }
  621.  
  622.  
  623. //****************************************************************************
  624. //***** The Function to Subtract x Days from a Given Date  *******************
  625. //****************************************************************************
  626. long MDates::SubDays(long date, int x)
  627. {
  628.     memset( cChk, '\0', sizeof(cChk));
  629.     memset( cTestDate, '\0', sizeof(cTestDate));
  630.     ltoa( date, Buffer, 10);
  631.  
  632.     lTestDate = Date2Julian( Buffer );
  633.     lTestDate = lTestDate - x;
  634.  
  635.     Julian2String( lTestDate, cTestDate );
  636.     DateFormat( cTestDate, cChk, MMDDYY);
  637.  
  638.     while( !Valid_Date(cChk) )  // Test for date validity
  639.     {
  640.         lTestDate--;
  641.         Julian2String( lTestDate, cTestDate );
  642.         DateFormat( cTestDate, cChk, MMDDYY);
  643.     }
  644.  
  645.     NewDate = atol( cTestDate );
  646.  
  647. return( NewDate );
  648. }
  649.  
  650.